package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo ){
        PageInfo<Employee> result  = employeeService.query(qo);
        model.addAttribute("result",result);

        System.out.println(result);

        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        return "employee/list";
    }
    @RequestMapping("/delete")
    public String delete(Long id ){
        if (id!=null){
            employeeService.delete(id);
        }
        return "redirect:/employee/list";
    }
    @RequestMapping("/input")
    public String input(Long id,Model model){

        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);

        List<Role> roles = roleService.listAll();
        model.addAttribute("roles",roles);

        if (id!=null){
            Employee employee = employeeService.select(id);
            model.addAttribute("employee",employee);
        }
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){
        if (employee.getId()!=null){
            employeeService.update(employee ,ids);
        }else {
            employeeService.insert(employee,ids);
        }
        return new JsonResult(true,"操作成功");
    }
    @RequestMapping("/bachDelete")
    @ResponseBody
    public JsonResult bachDelete( Long[] ids){
     employeeService.bachDelete(ids);
     return new JsonResult();
    }
    @RequestMapping("/checkName")
    @ResponseBody
    public HashMap checkName(String name){
        Employee employee = employeeService.selectByName(name);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("valid",employee==null);
        return map;
    }
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //创建excel文件
        Workbook wb = employeeService.exportXls();
        //把excel的数据输出给浏览器
        wb.write(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws Exception {
        employeeService.importXls(file);
        return new JsonResult();
    }

}
